﻿CREATE PROCEDURE [usix].[s_Export_ALL]
WITH EXECUTE AS 'AFC-HOME\SQLServer'
AS
SET IMPLICIT_TRANSACTIONS OFF
SET NOCOUNT ON
DECLARE curEFM CURSOR
READ_ONLY FOR
SELECT E.[Customer #],E.CompanyID FROM usix.t_CustomerFile_Export E
LEFT OUTER JOIN t_Borrower B ON E.SSN = B.SSN
WHERE [Customer #] > '' AND
			--E.[Primary SSN] >= '0' AND
			(E.[Primary SSN] like '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]') AND
			ISNULL(ApplID,0) < 1000000 /*AND B.ID IS NULL*/ AND
			dbo.clr_VINCheck(E.VIN) = 1
AND			dbo.SSNCheck(E.SSN) = 1
ORDER BY [G_L Date] DESC
DECLARE @UsixID varchar(40),@CompanyID int
OPEN curEFM

FETCH NEXT FROM curEFM INTO @UsixID,@CompanyID
WHILE @@fetch_status = 0 BEGIN
	BEGIN TRAN
	EXEC usix.s_Export @UsixID,@CompanyID
	IF @@ERROR <> 0 ROLLBACK TRAN ELSE COMMIT
	--BREAK
	IF dbo.Stop() = 1 BREAK
	FETCH NEXT FROM curEFM INTO @UsixID,@CompanyID
END

CLOSE curEFM
DEALLOCATE curEFM
--AF100805
--GO
--BEGIN TRAN
--EXEC usix.s_Export_ALL
--ROLLBACK TRAN
--SELECT dbo.VINCheck('KMHVD34N1VU279925')
--										 KMHVD34N1VU2799259







